package com.arqui.admmon_module.server.services;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.xml.rpc.ServiceException;

import com.arqui.admmon_module.server.webservices.eventsmonitor.File;
import com.arqui.admmon_module.server.webservices.eventsmonitor.NonexistentEntityException;
import com.arqui.admmon_module.server.webservices.eventsmonitor.UnknownHostException;
import com.arqui.admmon_module.server.webservices.eventsmonitor.WebServices_PortType;
import com.arqui.admmon_module.server.webservices.eventsmonitor.WebServices_ServiceLocator;
import com.arqui.admmon_module.shared.Constants;
import com.arqui.admmon_module.shared.HistoricLoadEntity;
import com.arqui.admmon_module.shared.RealTimeLoadEntity;


public class FileLoadService {

	//To find files uploaded in historical mode
	public List<HistoricLoadEntity> historicLoadService(Date initDate,
			Date finalDate) throws Exception, UnknownHostException,
			NonexistentEntityException, RemoteException, ServiceException {
		Calendar date1 = Calendar.getInstance();
		Calendar date2 = Calendar.getInstance();
		date1.setTime(initDate);
		date2.setTime(finalDate);
		String initialDate = dateToString(date1);
		String endDate = dateToString(date2);
		
		List<HistoricLoadEntity> finalList = new ArrayList<HistoricLoadEntity>();
		List<File> fileList;
		fileList = getFiles(initialDate, endDate);
		while (!fileList.isEmpty()) {
			HistoricLoadEntity h = new HistoricLoadEntity();
			//To check if there is an initial event and an final event to a same file			
			int i = 1;
			while (i < fileList.size()) {
				if (fileList.get(0).getId().equals(fileList.get(i).getId())) {
					h.setId(fileList.get(0).getId());
					h.setStartDate(fileList.get(0).getDate().toString());
					h.setEndDate(fileList.get(i).getDate().toString());
					h.setStore((fileList.get(0).getStore()));
					h.setBranch(fileList.get(0).getBranch());
					h.setState("Completado");
					finalList.add(h);
					fileList.remove(i);
					fileList.remove(0);
				} else {
					i++;
				}
			}
			
			if (!fileList.isEmpty()) {
				//To check if it's an initiation event
				if (fileList.get(0).getDescription()
						.equals(Constants.STATE_INIT)) {
					h.setId(fileList.get(0).getId());
					h.setStartDate(fileList.get(0).getDate().toString());
					h.setEndDate("---");
					h.setStore(fileList.get(0).getStore());
					h.setBranch(fileList.get(0).getBranch());
					h.setState("Cargando...");
					finalList.add(h);
					fileList.remove(0);
				}
				//When it's a final event
				else {
					h.setId(fileList.get(0).getId());
					h.setStartDate("---");
					h.setEndDate(fileList.get(0).getDate().toString());
					h.setStore(fileList.get(0).getStore());
					h.setBranch(fileList.get(0).getBranch());
					h.setState("Completado, Fecha de inicio anterior a la consulta");
					finalList.add(h);
					fileList.remove(0);
				}
			}
		}
		return finalList;
	}

	//To find files uploaded in real time mode
	public List<RealTimeLoadEntity> realLoadService() throws UnknownHostException,
			NonexistentEntityException, RemoteException, ServiceException, Exception {
		List<RealTimeLoadEntity> finalList = new ArrayList<RealTimeLoadEntity>();
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = finalDate;
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 5);
		String initialDate = dateToString(initDate);
		String endDate = dateToString(finalDate);

		List<File> list = getFiles(initialDate, endDate);
		for (File f : list) {
			RealTimeLoadEntity r = new RealTimeLoadEntity();
			r.setId(f.getId());
			r.setBranch(f.getBranch());
			r.setDate(f.getDate().toString());
			r.setDescription(f.getDescription());
			r.setStore(f.getStore());
			finalList.add(r);
		}

		return finalList;
	}

	private String dateToString(Calendar date) {
		String stringDate = "" + date.get(Calendar.YEAR) + "-"
				+ date.get(Calendar.MONTH) + "-"
				+ date.get(Calendar.DAY_OF_MONTH) + "-"
				+ date.get(Calendar.HOUR_OF_DAY) + "-"
				+ date.get(Calendar.MINUTE) + "-" + date.get(Calendar.SECOND);
		return stringDate;
	}

	private List<File> getFiles(String initDate, String endDate)
			throws ServiceException, Exception, UnknownHostException,
			NonexistentEntityException, RemoteException {
		WebServices_ServiceLocator service = new WebServices_ServiceLocator();
		WebServices_PortType port = service.getWebServicesPort();
		return arrayToList(port.getFiles(initDate, endDate));
	}

	private List<File> arrayToList(File[] files) {
		List<File> list = new ArrayList<File>();
		for (int i = 0; i < files.length; i++)
			list.add(files[i]);
		return list;
	}
}
